package io.renren.modules.pachong.util;

import io.renren.modules.pachong.Funny;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * html解析器
 * 先将网页另存到本地，然后将主页面文件解析
 * @author Peter
 * @date 2018-8-8
 */
public class HtmlParser {

    public static List<Funny> funny(String str) throws FileNotFoundException {
        List<Funny> list = new ArrayList<>();
        Document doc = Jsoup.parse(str, "utf-8");
        Elements articles = doc.getElementsByTag("article");
        int len = articles.size();
        for (int i = 0; i < len; i++) {
            Element article = articles.get(i);
            Elements ps = article.getElementsByTag("p");
            int psLen = ps.size();
            if(psLen == 0){
                continue;
            }else  {
                Element second = ps.get(psLen-1);
                Elements ad = second.getElementsByAttributeValue("data-tag", "tg");
                if (ad.size()>0) {
                    // 去广告
                    continue;
                }
            }
            Element h1 = article.getElementsByTag("h1").first();
            Element a = h1.child(0);
            String href = a.attr("href");
            if(!href.contains("sh.qihoo.com")){
                // 去广告
                continue;
            }
            String title = a.text();
            Element first = ps.first();
            Elements imgs = first.getElementsByTag("img");
            String urls = "";
            if (imgs != null) {
                int imgLen = imgs.size();
                for (int j = 0; j < imgLen; j++) {
                    Element img = imgs.get(j);
                    String imgHref = img.attr("src").substring(1);
                    String[] arr = imgHref.split("/");
                    urls += "https://p0.ssl.qhimgs4.com/dmfd/158_100_//"+arr[arr.length-1]+ ",";
                }
                if (imgLen > 0) {
                    urls = urls.substring(0, urls.length() - 1);
                }
            }
            Funny f = new Funny();
            f.setTitle(title);
            f.setHref(href);
            f.setImgUrls(urls);
            list.add(f);
        }
        return list;
    }

    public static String getXmlStr(InputStream in) {
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String strRead = "";
            StringBuilder sb = new StringBuilder();
            while (null != (strRead = br.readLine())) {
                sb.append(strRead);
            }
            br.close();
            return sb.toString();
        } catch (IOException e) {
            e.printStackTrace();
            return "";
        }
    }

    public static void main(String[] args) {
        try {
//            Document doc = Jsoup.connect("https://sh.qihoo.com/991755187ebb2e929?refer_scene=&scene=1&sign=look&uid=56df8866f43232d99c745a02f52ebc72").get();
            Document doc = Jsoup.connect("http://www.sihaidy.com/list/1_1.html").get();
            System.out.print(doc.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
